package poler.control.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.salesforce.results.LoginResult;
	
	import mx.rpc.IResponder;
	import mx.utils.ObjectUtil;
	
	import poler.business.LoginServiceDelegate;
	import poler.classes.AbstractCommand;
	import poler.classes.ViewReference;
	import poler.control.events.LoginEvent;
	import poler.model.ModelLocator;
	import poler.util.ProgressHandler;

	
	
	public class LoginCmd extends AbstractCommand implements ICommand, IResponder
	{
		private var _amLocator:ModelLocator = ModelLocator.getInstance();
		private var func:Function;
		public function execute(event:CairngormEvent):void
		{
			ProgressHandler.IsBusyView();					
			if(LoginEvent(event).Callback != null) func = LoginEvent(event).Callback;
			if(LoginEvent(event).CustomErrorString != null) CustomErrorMessage = LoginEvent(event).CustomErrorString; 
			
			var delegate:LoginServiceDelegate = new LoginServiceDelegate(this);
			delegate.login(LoginEvent(event).Un,LoginEvent(event).Pw);
		}
		override public function fault(info:Object):void{
			ProgressHandler.IsNotBusyView();
		}
		public function result(data:Object):void
		{
			trace('result:\n'+ObjectUtil.toString(data));
			ProgressHandler.IsNotBusyView();
			var result:LoginResult = data as LoginResult;
			_amLocator.loginDetails = result;
			
			if(func != null) func(data);
			//
			_amLocator.isLoggedIn = true;
			ViewReference.ViewChange(ViewReference.HOME_VIEW);
			//
			
		}
		
	}
}